<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Redis;

use App\Services\RedisService;
use App\Repos\UserRepo;

use App\Repos\OrderRepo;

use App\Repos\CouponRepo;
use Cache,OSS,Plupload;
use Carbon\Carbon;
use EasyWeChat;
use App\Services\WechatTpl;
use App\Models\ProductImg;
use Intervention\Image\ImageManagerStatic as Image;

class IndexController extends Controller
{
    public function dashboard()
    {
        return redirect('zadmin/products');
    }

    public function index()
    {
    	
    }

    public function upload()
    {
        return Plupload::receive('file', function ($file)
        {
            $filename = uniqid().'.'.$file->extension();
            $image_resize = Image::make($file->getRealPath());              
            $image_resize->resize(500, 500);
            $image_resize->save(public_path('uploads/thumb_' .$filename));
            
            $thumb_content = file_get_contents(public_path('uploads/thumb_' .$filename));
            OSS::putObject("xmistz", 'thumb_' .$filename, $thumb_content);
            unlink(public_path('uploads/thumb_' .$filename));

            $filecontent = file_get_contents($file->getRealPath());            
            
            OSS::putObject("xmistz", $filename, $filecontent);
            $thumb  = 'thumb_' .$filename;
            $data   = $filename;
            $status = true;
            return compact('data','status','thumb');
        });

    }

    public function imgUpload(Request $request)
    {
        $product_id = $request->product_id;
        return Plupload::receive('file', function ($file) use ($product_id)
        {
           
           
            
            $filename = uniqid().'.'.$file->extension();

            $image_resize = Image::make($file->getRealPath());              
            $image_resize->resize(500, 500);
            $image_resize->save(public_path('uploads/thumb_' .$filename));
            
            $thumb_content = file_get_contents(public_path('uploads/thumb_' .$filename));
            OSS::putObject("xmistz", 'thumb_' .$filename, $thumb_content);
            unlink(public_path('uploads/thumb_' .$filename));

            $filecontent = file_get_contents($file->getRealPath());
            OSS::putObject("xmistz", $filename, $filecontent);
            $data = $filename;
            $status = true;

            $img['product_id'] = $product_id;
            $img['thumb']      = 'thumb_'.$filename;
            $img['imgurl']     = $filename;
            $rs = ProductImg::create($img);
            $img_id = $rs->id;
            return compact('data','status','img_id');
        });

    }
}
